# coding: utf-8
'''
Created on 2009-7-20

@author: roy
'''


import time
from common.httpHepler import *
from constant import *
from common.util import *


class ranch:
    '''
    kaixin ranch
    '''


    def __init__(self, me, http, verify):
        '''
        Constructor
        '''
        self.__http = http
        self.__me = me
        self.__verify = verify
        
        
    def harvest(self):
        #log('-'*20 + ' ranch ' + '-'*20)
        
        mfList = dict()
        mfList[self.__me] = 'mySelf'
        
        url = '/house/ranch/getfriendproduct2.php?verify=' + self.__verify
        content = self.__http.read_page(url)
        if (content != None) and ( content != ''): 
            regex = u'<a href=\"/house/ranch/\\?[^>]*uid=([^;&]*)[^>]*>([^<]*)</a>[^\u53EF\u5077]*\u53EF\u5077'
            mlist = search_text(regex, content, True)
            if len(mlist) > 0:
                for i in range(len(mlist)):
                    mfList[mlist[i][0]] = mlist[i][1]
        
        for uid, name in mfList.items():
            if self.__me == uid:
                url = '/house/ranch/?uid=%s&verify=%s'
                url = url % (uid, self.__verify)
            else:
                url = '/house/ranch/?verify=%s&fuid=%s'
                url = url % (self.__verify, uid)
             
            #log(url)
                
            content = self.__http.read_page(url)
            
            if (content == None) or (content == ''):
                continue
            
            if content.find(u'\u5de1\u67e5\u5458') > -1: # 巡查员
                continue
              
            content = content.replace(u"\u5DF2\u5077\u8FC7", u"\u0001") # 已偷过
            content = content.replace(u"\u53EF\u5077", u"\u0002") # 防偷期
            content = content.replace(u"\u732A\u4ED4", u"\u0003") # 猪仔
            content = content.replace(u"\u6536\u83B7", u"\u0004") #收获
            
            harvestList = dict()
            
            if self.__me == uid:
                #matchList = search_text(u'<div class=\"list\">\\s*([^:]{1,5}):.*skey=(\\w+?)&amp;type=(\\d+?)', content, True)
                regex = '<a[^>]*href=\"/house/ranch/havest.php?[^>]*skey=([^&]*)&[^>]*type=(\\d*)[^>]*>'
                matchList = search_text(regex, content, True)
                count = len(matchList)
                if count > 0:
                    for i in range(count):
                        #harvestList[matchList[i][0]] = (matchList[i][1], matchList[i][2]) 
                        harvestList[matchList[i][0]] = matchList[i][1]
            else:
                #matchList = search_text(u'<div class=\"list\">\\s*([^:]{1,5}):.*[^\u0001\u0002].*skey=(\\w+?)&amp;type=(\\d+?)', content, True)
                regex = '<a[^>]*href=\"/house/ranch/havest.php?[^>]*skey=([^&]*)&[^>]*type=(\\d*)[^>]*>'
                matchList = search_text(regex, content, True)
                count = len(matchList)
                if count > 0:
                    for i in range(count):
                        #harvestList[matchList[i][0]] = (matchList[i][1], matchList[i][2]) 
                        harvestList[matchList[i][0]] = matchList[i][1]
            
            count = len(harvestList)
            if count > 0:
                for key, value in harvestList.items():
                    harvestUrl = '/house/ranch/havest.php?skey=%s&type=%s&verify=%s&fuid=%s'
                    #harvestUrl %= (value[0], value[1], self.__verify, uid)
                    harvestUrl %= (key, value, self.__verify, uid)
                    #log(harvestUrl)
                    content = self.__http.read_page(harvestUrl)
                    log('Harvest %s, %s' % (name, key))
                    
            #time.sleep(3)
            
        #log('-'*18 + ' end ranch ' + '-'*18)
            
            